package com.gxa.service.impl.system;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.mapper.system.UserRoleMapper;
import com.gxa.pojo.entity.system.User;
import com.gxa.pojo.entity.system.UserRole;
import com.gxa.service.system.UserRoleService;
import com.gxa.utils.QueryResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author lch
 * @Date 2024/2/22 15:52
 * @Description 员工添加角色管理
 **/
@Service
public class UserRoleServiceImpl implements UserRoleService {

    @Resource
    private UserRoleMapper userRoleMapper;

    /**
     * 用户添加角色
     * @param userRoleList
     */
    @Override
    public void addUserRole(List<UserRole> userRoleList) {
        if (userRoleList.size() > 0) {
            UserRole userRole = userRoleList.get(0);
            delete(userRole.getUserId());
            userRoleMapper.insert(userRoleList);
        }
    }

    /**
     * 查询用户拥有的角色
     * @param userRole
     * @return
     */
    @Override
    public QueryResult queryUserRole(UserRole userRole) {
        Page<UserRole> page = PageHelper.startPage(userRole.getPage(), userRole.getLimit());

        List<UserRole> userRoleList = userRoleMapper.query(userRole);

        QueryResult queryResult = new QueryResult();
        queryResult.setData(userRoleList);
        queryResult.setCount(page.getTotal());
        return queryResult;
    }


    /**
     * 删除id这个用户的全部角色
     * @param id
     */
    private void delete(Integer id) {
        userRoleMapper.delete(id);
    }
}
